package AudioUDP.Sender;

/**
 * Describe class Recorder here.
 *
 *
 * Created: Tue Mar 23 10:44:18 2010
 *
 * @author <a href="mailto:stg1@pc111-1.formation-continue.grenoble-inp.fr">stagiaire</a>
 * @version 1.0
 */

import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;



public class SenderUp extends Thread{

    /**
     * Creates a new <code>Recorder</code> instance.
     *
     */
	final static int TAILLE_MAX_BUFFER=100;
    FileInputStream fis;
    BufferedInputStream ais;
    boolean  isOk = true;
    File fichier;
    DatagramPacket outPacket = null;
    SenderDown senderDown= null;    
    String hostname;

     SenderUp(String hostname) {
		try{
			this.hostname=hostname;
			fichier  = new File("E:\\Users\\Arnaud\\Downloads\\WindowsXPDemarrage.wav");
		    fis = new FileInputStream(fichier);						
	
		}catch(Exception e){
		    System.out.println(e);

		}
    }

    public void start() {
    	int compteur=0;
	// socket crée sur un port disponible choisi pas l'OS
    	byte [] buf = new byte[640];
    	int n=0;
    	
    	try{
			DatagramSocket datagramSocket = new DatagramSocket();
			InetAddress addr = InetAddress.getByName(hostname);
			senderDown= new SenderDown(datagramSocket,isOk);

			for(int i =0;i<44;i++)	//suppression de l'entête
				n=fis.read();
		   
			
		    while (n!=-1 ){
		    	synchronized (this){
			    	while (!isOk){
			    		wait();
		    	}}
		    	n=fis.read(buf);
		    	System.out.println(compteur++);
				outPacket = new DatagramPacket( buf, buf.length,addr, 2510);
				 datagramSocket.send(outPacket);
				 
				 Thread.sleep(2);		 
		    }
	    
		    fis.close();
		    datagramSocket.close();
		    System.out.println("envoi terminé");
    	}catch(Exception e){
		    System.out.println(e);
		    System.out.println("tuto");
		    }
      }

    private synchronized boolean isOK(){
    	return isOk;
    } 
}
